<template>
    <div class="publicpageview">
        <div class="publicformview">
            <el-form :inline="true" :model="listQuery" size="mini" ref="searchForm" label-width="100px">
                <el-row :gutter="20">

                    <el-col :span="4">
                        <el-form-item label="管理单位：" prop="districtId">
                            <el-select v-model="listQuery.districtId" clearable @change="handleQuchange">
                                <el-option v-for="item in areaList" :label="item.dname" :value="item.did"
                                    :key="item.did"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="管理部：" prop="houseAdminId">
                            <el-select v-model="listQuery.houseAdminId" clearable>
                                <el-option v-for="item in houseAdminList" :label="item.aname" :value="item.aid"
                                    :key="item.aid"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="4">
                        <el-form-item label="工程编号：" prop="keyword">
                            <el-input v-model="listQuery.keyword"></el-input>
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="4">
                        <el-form-item label="所编号：" prop="blockId">
                            <el-input v-model="listQuery.blockId"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="所地址：" prop="blockAddress">
                            <el-input v-model="listQuery.blockAddress"></el-input>
                        </el-form-item>
                    </el-col>


                    <el-col :span="4">
                        <el-form-item label="承租人：" prop="renter">
                            <el-input v-model="listQuery.renter"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="是否办妥：" prop="isBantuo">
                            <el-select v-model="listQuery.isBantuo">
                                <el-option label="是" :value="1" :key="1"></el-option>
                                <el-option label="否" :value="0" :key="0"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <div style="padding-left: 90px;">
                            <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
                            <el-button size="mini" @click="resetForm('searchForm')">重置</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <div class="publitableview">
            <el-button type="primary" size="mini" @click="handleAddUser('add')">工单录入</el-button>
            <el-table :data="list" border fit highlight-current-row size="mini">
                <el-table-column align="center" label="区房屋管理单位" prop="districtName" min-width="200">
                </el-table-column>
                <el-table-column align="center" label="管理部（分中心）" prop="houseAdminName" min-width="150">
                </el-table-column>
                <el-table-column align="center" label="管片代码" prop="blockGroupName">
                </el-table-column>
                <el-table-column align="center" label="所编号" prop="blockId">
                </el-table-column>
                <el-table-column align="center" label="所地址" prop="blockAddress">
                </el-table-column>
                <el-table-column align="center" label="维修地址" prop="repairAddress">
                </el-table-column>
                <el-table-column align="center" label="维修工单" prop="repairOrderNumber">
                </el-table-column>
                <el-table-column align="center" label="承租人" prop="renter">
                </el-table-column>
                <el-table-column align="center" label="承租人电话" prop="renterPhone" min-width="130">
                </el-table-column>
                <el-table-column align="center" label="合同号" prop="contractNo">
                </el-table-column>
                <el-table-column align="center" label="维修日期" prop="repairDate">
                    <template slot-scope="scope">
                        {{getTime(scope.row.repairDate)}}
                    </template>
                </el-table-column>
                <el-table-column align="center" label="维修部门" prop="repairLocation">
                </el-table-column>
                <el-table-column align="center" label="维修项目" prop="repairItems">
                </el-table-column>
                <el-table-column align="center" label="维修单位" prop="maintenanceOrganization">
                </el-table-column>
                <el-table-column align="center" label="维修单位电话" prop="maintenanceOrganizationPhone" min-width="150">
                </el-table-column>
                <el-table-column align="center" label="完工情况" prop="completionStatus">
                </el-table-column>
                <el-table-column align="center" label="备注" prop="note">
                </el-table-column>
                <el-table-column align="center" label="操作" width="200" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" v-if="scope.row.isBantuo == 0" size="mini"
                            @click="handleBantuo(scope.row)">办妥</el-button>
                        <el-button type="text" size="mini">打印</el-button>

                        <el-button type="text" size="mini" @click="handleUpdate(scope.row, 'update')">修改</el-button>
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row,'details')">详情</el-button>
                        <el-button type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="publicpaginationview">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="listQuery.pageNum" background :page-sizes="[10, 20, 50, 100]"
                    :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
                    small></el-pagination>
            </div>
        </div>

        <el-dialog :title="isAdd == 'add' ? '维修工单录入' : isAdd == 'update' ? '维修工单修改' : '维修工单详情'"
            :visible.sync="dialogVisible" width="60%" :before-close="handleClose">
            <el-form :model="formData" ref="dataForm" class="demo-ruleForm" label-position="right" label-width="150px"
                size="mini">
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="区房屋管理单位：" prop="districtId">
                            <el-select v-model="formData.districtId" @change="handleFormQuchange">
                                <el-option v-for="item in areaList" :label="item.dname" :value="item.did"
                                    :key="item.did"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="管理部(分中心)：" prop="houseAdminId">
                            <el-select v-model="formData.houseAdminId" @change="handleHouseChange">
                                <el-option v-for="item in houseFormAdminList" :label="item.aname" :value="item.aid"
                                    :key="item.aid"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="管片代码：" prop="blockGroupId">
                            <el-select v-model="formData.blockGroupId" @change="handleGuanpianChange">
                                <el-option v-for="item in guanpianList" :label="item.groupnew" :value="item.groupnumber"
                                    :key="item.groupnumber"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="所号：" prop="blockId">
                            <el-select v-model="formData.blockId" @change="handleSuoChange">
                                <el-option v-for="item in suoList" :label="item.creatingdeptno" :value="item.blockid"
                                    :key="item.blockid"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="所地址：" prop="blockAddress">
                            <el-input v-model="formData.blockAddress"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="维修地址：" prop="repairAddress">
                            <el-input v-model="formData.repairAddress"></el-input>
                        </el-form-item>
                    </el-col>


                    <el-col :span="8">
                        <el-form-item label="维修单号：" prop="repairOrderNumber">
                            <el-input v-model="formData.repairOrderNumber"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="承租人：" prop="renter">
                            <el-input v-model="formData.renter"></el-input>
                        </el-form-item>
                    </el-col>


                    <el-col :span="8">
                        <el-form-item label="承租人电话：" prop="renterPhone">
                            <el-input v-model="formData.renterPhone"></el-input>
                        </el-form-item>
                    </el-col>


                    <el-col :span="8">
                        <el-form-item label="合同号：" prop="contractNo">
                            <el-input v-model="formData.contractNo"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="维修日期：" prop="repairDate">
                            <el-date-picker v-model="formData.repairDate" type="date" placeholder="年/月/日">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="维修部位：" prop="repairLocation">
                            <el-input v-model="formData.repairLocation"></el-input>
                        </el-form-item>
                    </el-col>


                    <el-col :span="8">
                        <el-form-item label="维修项目：" prop="repairItems">
                            <el-input v-model="formData.repairItems"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="维修单位：" prop="maintenanceOrganization">
                            <el-input v-model="formData.maintenanceOrganization"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="维修单位电话：" prop="maintenanceOrganizationPhone">
                            <el-input v-model="formData.maintenanceOrganizationPhone"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="完工情况：" prop="completionStatus">
                            <el-input v-model="formData.completionStatus"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="备注：" prop="note">
                            <el-input v-model="formData.note"></el-input>
                        </el-form-item>
                    </el-col>

                </el-row>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleClose" size="medium" v-if="isAdd == 'details'">返
                    回</el-button>

                <el-button type="primary" @click="handleSubmitClick" size="medium" v-else>保 存</el-button>
            </span>
        </el-dialog>



        <el-dialog title="维修工单办妥" :visible.sync="bantuoVisible" width="40%" :before-close="handleBantuoClose">
            <el-form :model="formBantuoData" ref="databantuoForm" class="demo-ruleForm" label-position="right"
                label-width="150px" size="mini">
                <el-form-item label="区房屋管理单位：" prop="userId">
                    <el-input v-model="formBantuoData.userId"></el-input>
                </el-form-item>
                <el-form-item label="管理部（分中心）：" prop="userName">
                    <el-input v-model="formBantuoData.userName"></el-input>
                </el-form-item>


                <el-form-item label="管片代码：" prop="remarks">
                    <el-input v-model="formBantuoData.remarks"></el-input>
                </el-form-item>

                <el-form-item label="所编号：" prop="remarks">
                    <el-input v-model="formBantuoData.remarks"></el-input>
                </el-form-item>

                <el-form-item label="所地址：" prop="remarks">
                    <el-input v-model="formBantuoData.remarks"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="bantuoVisible = false" size="medium">保 存</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { listBrokenRepairOrder, createBrokenRepairOrder, updateBrokenRepairOrder, deleteBrokenRepairOrder, banTuoBrokenRepairOrder } from '@/api/repair'
import { getArea, getHouseAdmin, getBlockGroup, getBlockByGroup, upload } from '@/api/common'
import { Message } from 'element-ui'
import moment from 'moment';
export default {
    name: "AccommodationManagementIndex",

    data() {
        return {
            listQuery: {
                pageNum: 1,
                pageSize: 20,
                districtId: "",
                houseAdminId: "",
                blockId: "",
                blockAddress: "",
                renter: "",
                isBantuo: null
            },
            total: 0,
            list: [

            ],
            dialogVisible: false,
            bantuoVisible: false,
            isAdd: 'add',
            formData: {
                districtId: "",
                houseAdminId: "",
                blockGroupId: "",
                blockId: "",
                blockAddress: "",
                repairAddress: "",
                repairOrderNumber: "",
                renter: "",
                renterPhone: "",
                contractNo: "",
                repairDate: "",
                repairLocation: "",
                repairItems: "",
                maintenanceOrganization: "",
                maintenanceOrganizationPhone: "",
                completionStatus: "",
                note: ""


            },
            formBantuoData: {},
            areaList: [],
            houseAdminList: [],
            houseFormAdminList: [],
            guanpianList: [],
            suoList: [],
        };
    },

    computed: {
        getTime() {
            return (val) => {
                if (val) {
                    return moment(val).format("YYYY-MM-DD")
                } else {
                    return ""
                }
            }
        }
    },

    mounted() {
        this.getCommon();
        this.getList();
    },

    methods: {

        handleSearch() {
            this.listQuery.pageNum = 1;
            this.getList();
        },

        getCommon() {
            getArea().then(res => {
                this.areaList = res.data
            })
        },

        handleQuchange(val) {
            this.listQuery.houseAdminId = null;
            if (val) {
                this.getHouseAdmin(val);

            } else {
                this.houseAdminList = [];
            }
        },

        handleFormQuchange(val) {
            this.formData.houseAdminId = null;
            this.handleHouseChange();
            if (val) {
                this.getFormHouseAdmin(val);

            } else {
                this.houseFormAdminList = [];
            }
        },

        getHouseAdmin(val) {
            getHouseAdmin({ DID: val, pageNum: 1, pageSize: 100000 }).then(res => {
                this.houseAdminList = res.data.list;
            })
        },
        getFormHouseAdmin(val) {
            getHouseAdmin({ DID: val, pageNum: 1, pageSize: 100000 }).then(res => {
                this.houseFormAdminList = res.data.list;
            })
        },

        handleHouseChange(val) {
            this.formData.blockGroupId = null
            this.handleGuanpianChange();
            if (val) {
                getBlockGroup({ houseAdminId: val, pageNum: 1, pageSize: 10000 }).then(res => {
                    this.guanpianList = res.data.list;
                })
            } else {
                this.guanpianList = [];
            }
        },

        handleGuanpianChange(val) {

            this.formData.blockId = null
            this.handleSuoChange();
            if (val) {
                getBlockByGroup({ group: val, pageNum: 1, pageSize: 10000 }).then(res => {
                    this.suoList = res.data.list;
                })
            } else {
                this.suoList = [];
            }
        },

        handleSuoChange(val) {
            console.log(val)
            if (val) {
                this.suoList.forEach(item => {
                    if (item.blockid == val) {
                        this.formData.blockAddress = item.address;

                    }
                })
            } else {
                this.formData.blockAddress = ""
            }
        },
        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },
        getList() {

            listBrokenRepairOrder(this.listQuery).then((response) => {
                this.total = response.data.total;
                this.list = response.data.list;

            });
        },

        resetForm(val) {

            this.$refs[val].resetFields();
        },

        handleAddUser(type) {
            this.isAdd = 'add';
            this.dialogVisible = true;
        },

        handleClose() {
            this.resetForm("dataForm");
            this.dialogVisible = false;
            this.houseFormAdminList = [];
            this.guanpianList = [];
            this.suoList = [];
        },

        handleSubmitClick() {
            const { formData } = this;
            if (this.isAdd == 'add') {
                createBrokenRepairOrder({ ...formData }).then(res => {
                    this.handleClose()
                    this.getList();
                    this.$message.success("操作成功")
                })
            }
            if (this.isAdd == 'update') {
                updateBrokenRepairOrder({ ...formData }).then(res => {
                    this.handleClose()
                    this.getList();
                    this.$message.success("操作成功")

                })
            }

        },

        handleBantuoClose() {
            this.resetForm("databantuoForm");
            this.bantuoVisible = false;

        },

        async handleUpdate(currentData, type) {
            this.isAdd = type;
            //   一定要在打开弹窗前 再赋值 不然resetFiles没用
            this.dialogVisible = true;
            await this.handleFormQuchange(currentData.districtId)
            await this.handleHouseChange(currentData.houseAdminId)
            await this.handleGuanpianChange(currentData.blockGroupId)
            this.$nextTick(() => {
                this.formData = { ...currentData };
            });
        },

        handleBantuo(currentData) {
            // this.bantuoVisible = true;
            // this.$nextTick(() => {
            //     this.formBantuoData = { ...currentData };
            // });

            this.$confirm(
                "是否办妥改工单？",
                "提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    banTuoBrokenRepairOrder(currentData.id).then(res => {
                        this.$message.success("操作成功")
                        this.getList();
                    })
                })
                .catch(() => { });
        },

        handleDelete(id) {
            this.$confirm(
                "此操作将删除该条内容，请确认是否继续操作？",
                "删除提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    deleteBrokenRepairOrder(id).then(res => {
                        this.$message.success("操作成功")
                        this.getList();
                    })
                })
                .catch(() => { });
        }
    },
};
</script>